package com.leon.androidcoursedesign.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.UUID;

public class ValueUtils {
    public static String formatTime(long time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        return simpleDateFormat.format(new Date(time));
    }

    /**
     * 保存note内容至文本文件
     *
     * @param context   context
     * @param content   note内容
     * @param noteId    noteId
     * @return  返回保存状态
     */
    public static boolean saveNote(Context context, String content, String noteId) {
        BufferedWriter bufferedWriter = null;
        try {
            File noteFile = new File(new File(context.getFilesDir(), "notes"), noteId + ".txt");
            if (!noteFile.exists()) {
                noteFile.createNewFile();
            }

            bufferedWriter = new BufferedWriter(new FileWriter(noteFile, false));
            bufferedWriter.write(content);
            bufferedWriter.flush();

            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return false;
    }

    /**
     * 读取note文本文件内容
     *
     * @param context   context
     * @param noteId    noteId
     * @return  返回note内容
     */
    public static String readNote(Context context, String noteId) {
        File noteFile = new File(new File(context.getFilesDir(), "notes"), noteId + ".txt");

        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(noteFile));

            String temp;
            StringBuilder content = new StringBuilder();
            while ((temp = bufferedReader.readLine()) != null) {
                content.append(temp);
            }

            return content.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }

    /**
     * 删除本地note文本文件
     *
     * @param context   context
     * @param noteId    noteId
     */
    public static void deleteNote(Context context, String noteId) {
        File noteFile = new File(new File(context.getFilesDir(), "notes"), noteId + ".txt");

        if (noteFile.exists()) {
            noteFile.delete();
        }
    }

    /**
     * 获取当前登录的ID
     *
     * @param context   context
     * @return  返回当前登录的ID
     */
    public static String getLoginId(Context context) {
        SharedPreferences preferenceValues = context.getSharedPreferences("preferenceValues", Activity.MODE_PRIVATE);
        return preferenceValues.getString("operationId", null);
    }

    public static String getRandomId() {
        return UUID.randomUUID().toString().substring(0, 8);
    }
}
